<?php


namespace app\common\library;


use app\common\Exception;
use think\Error;

class HttpCurl
{
    public static function get($url, $param = array())
    {
        if (!is_array($param)) {
            throw new \think\Exception("参数必须为array");
        }
        $p = '';
        foreach ($param as $key => $value) {
            $p = $p . $key . '=' . $value . '&';
        }
        if (preg_match('/\?[\d\D]+/', $url)) {//matched ?c
            $p = '&' . $p;
        } else if (preg_match('/\?$/', $url)) {//matched ?$
            $p = $p;
        } else {
            $p = '?' . $p;
        }
        $p = preg_replace('/&$/', '', $p);
        $url = $url . $p;
        //echo $url;
        $httph = curl_init($url);
        curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
//        curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

//        curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($httph, CURLOPT_HEADER, 1);
        $rst = curl_exec($httph);
        setLog(__METHOD__,$rst);
        curl_close($httph);
        return $rst;
    }

    /*
     * post method
     */
    public static function post($url, $param ="")
    {
        $httph = curl_init($url);
        curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
//        curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        curl_setopt($httph, CURLOPT_POST, 1);//设置为POST方式
        curl_setopt($httph, CURLOPT_POSTFIELDS, $param);
//        curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($httph, CURLOPT_HEADER, 1);
        $rst = curl_exec($httph);
        curl_close($httph);
        return $rst;
    }
    public static function postFile($url,$data)
    {
        set_time_limit(0);
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $data,
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        return  $response;
    }
    public static function encrypt($key,$string)
    {
        if (strlen($string) % 16) {
            $str_padded = str_pad($string,strlen($string) + 16 - strlen($string) % 16, "\0");
        }
        // openssl_encrypt 加密不同Mcrypt，对秘钥长度要求，超出16加密结果不变
        $data = openssl_encrypt($str_padded, 'AES-128-ECB', $key, OPENSSL_NO_PADDING);
        $data = strtolower(bin2hex($data));
        return $data;
    }
    /**
     * @param string $string 需要解密的字符串
     * @return string
     */
    public static function decrypt($key,$string)
    {
        $decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_NO_PADDING);
        return $decrypted;
    }

    function curl_request($url,$post='',$cookie='', $returnCookie=0){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
//    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
//    curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
        if($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        }
        if($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        if($returnCookie){
//            list($header, $body) = explode("\r\n\r\n", $data, 2);
//            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
//            $info['cookie']  = substr($matches[1][0], 1);
//            $info['content'] = $body;
//            return $info;
            return $data;
        }else{
            return $data;
        }
    }


}